package com.game;

import java.text.SimpleDateFormat;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;

import com.young.R;

public class TimeManager {

	private static final String TAG = TimeManager.class.getSimpleName();
	private Context context = null;
	
	private Bitmap bmp_xyy = null;
	private Bitmap bmp_htl = null;
	
	private Bitmap bmp_time = null;
	private Bitmap bmp_time_background = null;
	private Bitmap bmp_time_dynamic = null;
	
	private NinePatch np_time = null;
	private NinePatch np_time_background = null;
	private NinePatch np_time_dynamic = null;
	
	private long totalTime = 1;
	private long leftTime = 1;
	private long intervalTime = 1;
	private CountTimer countTimer = null;
	
	private static final String DATE_FORMAT = "mm:ss:SSS";
	private SimpleDateFormat dateFormat = null;
	
	private RectF location_time = null;
	private RectF location_background = null; 
	private RectF location_dynamic = null; 
	
	TimeManager(Context context) {
		this.context = context;
		dateFormat = new SimpleDateFormat(DATE_FORMAT);
		
		location_time = new RectF();
		location_background = new RectF();
		location_dynamic = new RectF();
		
		loadBmp();
	}
	
	public void loadBmp() {
		if (bmp_xyy == null) {
			bmp_xyy = BitmapFactory.decodeResource(context.getResources(), R.drawable.img_xyy);
		}
		if (bmp_htl == null) {
			bmp_htl = BitmapFactory.decodeResource(context.getResources(), R.drawable.img_htl);
		}
		
		if (bmp_time == null) {
			bmp_time = BitmapFactory.decodeResource(context.getResources(), R.drawable.img_time);
			np_time = new NinePatch(bmp_time, bmp_time.getNinePatchChunk(), null);
			//bmp_time.recycle();
			
		}
		if (bmp_time_background == null) {
			bmp_time_background = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_time_background);
			np_time_background = new NinePatch(bmp_time_background, bmp_time_background.getNinePatchChunk(), null);
			//bmp_time_background.recycle();
		}
		if (bmp_time_dynamic == null) {
			bmp_time_dynamic = BitmapFactory.decodeResource(context.getResources(), R.drawable.img_time_dynamic);
			np_time_dynamic = new NinePatch(bmp_time_dynamic, bmp_time_dynamic.getNinePatchChunk(), null);	
			//bmp_time_dynamic.recycle();
		}
	}
	
	public void setTimerMaxTime(long time) {
		totalTime = time;
		leftTime = time;
	}
	
	public void setTimerIntervalTime(long time) {
		intervalTime = time;
	}
	
	public void startTimer() {
		if (countTimer == null) {
			countTimer = new CountTimer(totalTime, intervalTime){

				@Override
				public void onTick(long millisUntilFinished) {
					leftTime = millisUntilFinished;
				}

				@Override
				public void onFinish() {
					leftTime = 0;
				}
			};
		}
	}
	
	public void startTimer(long maxTime, long intervalTime) {
		setTimerMaxTime(maxTime);
		setTimerIntervalTime(intervalTime);
		
		if (countTimer == null) {
			countTimer = new CountTimer(totalTime, intervalTime){

				@Override
				public void onTick(long millisUntilFinished) {
					leftTime = millisUntilFinished;
					//Log.v(TAG, "Time Left " + leftTime);
				}

				@Override
				public void onFinish() {
					leftTime = 0;
				}
			};
		}
		if (countTimer != null) {
			countTimer.start();
		}
	}
	
	public void pauseTimer() {
		if (countTimer != null) {
			countTimer.pause();
		}
	}
	
	public void restartTimer() {
		if (countTimer != null) {
			countTimer.restart();
		}
	}
	
	public void cancelTimer() {
		if (countTimer != null) {
			countTimer.cancel();
			countTimer = null;
		}
	}
		
	public boolean TimerOver() {
		if (leftTime == 0) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public void setShowArea(float left, float top, float right, float bottom) {
		float tmpSwap = 0;
		if (left > right) {
			tmpSwap = left;
			left = right;
			right = tmpSwap;
		}
		
		if (top > bottom) {
			tmpSwap = top;
			top = bottom;
			bottom = tmpSwap;
		}
		
		if (right - left < bmp_time.getWidth()) {
			right = left + bmp_time.getWidth();
		}
		if (bottom - top < bmp_time.getHeight()) {
			bottom = top + bmp_time.getHeight();
		}
		float tmpRight = left + bmp_time.getWidth() + bmp_xyy.getWidth() / 2;
		location_time.set(left, top, tmpRight, bottom);
		location_background.set(tmpRight, top, right - tmpRight, bottom);
		location_dynamic.set(tmpRight, top, right - tmpRight, bottom);
	} 

	public void setShowArea(RectF rect) {
		setShowArea(rect.left, rect.top, rect.right, rect.bottom);
	}
	
	public void drawTime(Canvas canvas, Paint paint) {
		
//		String leftTimeStr = dateFormat.format(new Date(leftTime));
//		paint.setTextSize(50);
//		paint.setTextSkewX(-0.5f);
//		canvas.drawText(leftTimeStr, 50, 50, paint);
		
		if (np_time != null) {
			np_time.draw(canvas, location_time);
		}
		
		if (np_time_background != null) {
			np_time_background.draw(canvas, location_background);
		}
		Log.v(TAG, "background width " + location_background.width());
		location_dynamic.set(location_background.left, 
							 location_background.top + (bmp_time_background.getHeight() - bmp_time_dynamic.getHeight()) / 2, 
							 location_background.left + bmp_time_dynamic.getWidth() + (leftTime / (float)totalTime) * (location_background.width() - bmp_time_background.getWidth()), 
							 location_background.bottom - (bmp_time_background.getHeight() - bmp_time_dynamic.getHeight()) / 2);
		if (np_time_dynamic != null) {
			np_time_dynamic.draw(canvas, location_dynamic);
		}
		
		canvas.drawBitmap(bmp_xyy, location_dynamic.left - bmp_xyy.getWidth() / 2, 
								   location_dynamic.top + location_dynamic.height() / 2 - bmp_xyy.getHeight() / 2, paint);
		canvas.drawBitmap(bmp_htl, location_dynamic.right - bmp_htl.getWidth() / 2,
								   location_dynamic.top + location_dynamic.height() / 2 - bmp_htl.getHeight() / 2, paint);
	}
	
	public void recycle() {
		bmp_xyy.recycle();
		bmp_htl.recycle();
		
		bmp_time.recycle();
		bmp_time_background.recycle();
		bmp_time_dynamic.recycle();
		
		bmp_time = null;
		bmp_time_background = null;
		bmp_time_dynamic = null;
		
		np_time = null;
		np_time_background = null;
		np_time_dynamic = null;
	}
}
